<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-10-03 17:09:03
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 加料单
 */

namespace App\Http\Services\Finance;


use App\Http\Services\Service;
use App\Repositories\CompanyBilltradeRepository;
use App\Repositories\CompanyLinkManRepository;
use App\Repositories\FinanceReceivingRepository;
use App\Repositories\FinanceVoucherRepository;
use Illuminate\Support\Facades\DB;

/**
 * @property FinanceReceivingRepository $repository
 * @property FinanceVoucherRepository $voucher
 */
class DebtorService extends Service
    {
    public $state = 1;
    public function boot(FinanceReceivingRepository $repository, FinanceVoucherRepository $voucher)
        {
        $this->repository = $repository->where("state", $this->state);
        $this->voucher    = $voucher;
        }


    public function list()
        {
        $data = $this->repository->condition()->getGrid();
        return $data;
        }
    protected function save()
        {
        $gdata = request();
        if (is_numeric($gdata['id']) && $gdata['id'] > 0) {
            $ddata = $this->repository->whereId($gdata['id'])->first();
            if ($ddata->status_id == 1) {
                fail("收款单:[{$ddata->code}]已审核！不能编辑！");
                }
            if ($ddata->status_id == 2) {
                fail("收款单:[{$ddata->code}]已记账！不能编辑！");
                }
            }
        $gdata          = $gdata->toArray();
        $gdata['state'] = $this->state;
        $gdata          = $this->repository->setACSign("DEBTOR")->autosave($gdata);
        $this->savebilltrade($gdata->billtrade, $gdata->company_id);
        return $gdata;
        }

    private function savebilltrade($billtrade, $company_id)
        {
        return CompanyBilltradeRepository::firstOrCreate(['name' => trim($billtrade), 'company_id' => $company_id]);
        }
    protected function saveList()
        {
        $hasIds = [];
        array_filter(request('collection'), function ($v) use (&$hasIds)
            {
            $status_id = $this->repository->whereId($v['id'])->value("status_id");
            if ($status_id != 0) {
                fail("修改收款单[{$v['code']}]失败！可能此收款单已审核！");
                }
            $hasIds[] = $this->repository->autosave($v)->id;
            });
        //删除未审核的
        $this->repository->whereCompany_id(request("company_id"))->whereStatus_id(0)->whereNotIn("id", $hasIds)->get()->filter(function ($v)
            {
            $v->delete();
            });
        return;
        }
    protected function audit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($gdata)
            {
            if ($gdata->status_id == 1) {
                fail("收款单:[{$gdata->code}]已审核！不能审核！");
                }
            if ($gdata->status_id == 2) {
                fail("收款单:[{$gdata->code}]已记账！不能审核！");
                }
            auditSave($gdata);
            $gdata->finance();
            return $gdata;
            });
        }
    protected function noaudit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($gdata)
            {
            if ($gdata->status_id == 0) {
                fail("收款单:[{$gdata->code}]未审核！不能反审！");
                }

            $gdata->status_id == 2 && $gdata->nofinance();
            auditSave($gdata, false);
            return $gdata;
            });
        }
    protected function delete()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id == 1) {
                fail("单据已审核！禁止删除");
                }
            if ($v->status_id == 2) {
                fail("单据已记账！禁止删除");
                }
            return $v->delete();
            });
        }
    }